import {get,getLandInfo} from '~/views/property-info-sys/building-manage/api/building'
import {getAllProject} from '~/views/property-info-sys/project-manage/api/project_manage'
import { mapState,mapGetters } from 'vuex'
import {getFiles, delFiles, checkFiles} from '@/api/operate-construction-contract'
import {downloadBlob} from '~/utils/index'
import {download} from "~/admin/api/common"
export default {
    name: 'building_look',
    created(){
        this.landUses = this.getDict('land_uses')
        this.streets = this.getDict('streets')
        this.communityNames = this.getDict('community_names')
        const loading = this.$loading({
            lock: true,
            text: '数据加载中'
        });

			 this.propertyChannels = this.getDict("property_channel")
        // 如果是内嵌页面预览隐藏头部和左侧菜单
        const { id, frame } =  this.$route.query
        this.current = this.$route.query.current
        this.projectId = this.$route.query.projectId
        let sourceType = this.$route.query.sourceType
        !this.isWebView && frame && this.$store.commit('resetWebViewStyle',true)
        this.id = id
        //通过id获取项目信息
        get(id).then( rep => {
            this.propForm = rep.data.data
            this.propForm.building.cityCode = this.propForm.building.areaCode.split('-')[0]
            this.propForm.building.countyCode = this.propForm.building.areaCode.split('-')[1]
            if (rep.data.data.floors !== undefined && rep.data.data.floors.length > 0) {
                this.getFiles(rep.data.data.floors[0].floorId);
            }
            loading.close()
        }).catch(e => {
            console.log(e)
            loading.close()
            this.$message({
                type: 'error',
                message: '数据获取失败!'
            })
        })

        if("2" == sourceType){
            getLandInfo(2 ,this.id).then(res => {
                if (res.data.code === 0 && res.data.data !== null) {
                    this.landInfo = res.data.data
                }
            })
        }else{
            this.handleGetLandInfo(1)
        }

        // if (this.current === '10') {
        //     this.handleGetLandInfo(1)
        // } else {
        //     this.handleGetLandInfo(1)
        // }
    },
    data() {
        return {
            propForm: {
                building: {
                    buildingName: '',
                    unitNum: '',
                    projectId: '',
                    completeTime: '',
                    roomType: '',
                    buildPrice: '',
                    geographicCoordinates: '',
                    groundArea: '',
                    undergroundArea: '',
                    totalSets: '',
                    floorArea: '',
                    areaCode: '',
                    areaName: '',
                    provinceCode: '',
                    cityCode: '',
                    countyCode: '',
                    address: '',
                    isDetachedHouse: '',
                    unitBuilding: '',
                    unitName: '',
                    groundFloors: '',
                    undergroundFloors: '',
                    totalFloors: '',
                    remarks: '',
                    communityName: '',
                    buildStreet: ''
                },
                floors: {

                },
                facilitiesName: '',
                projectName: '',
            },
            landInfo: {
                address: '',
                addressInfo: '',
                landContractNum: '',
                landId: '',
                landUse: '',
                parcelArea: '',
                parcelNum: '',
                remarks: ''
            },
            busId: '',
            module: 'operate',
            loadFileList: [],
            landUses: [],
            current: '10',
            streets: [],
            communityNames: [],
            projectId: '',
					  propertyChannels:[]
        }
    },
    computed: {
        ...mapState(['isWebView']),
       /* street_ch: function(){
            const street = getDict('streets')
            let val = this.propForm.building.buildStreet
            if(val && street.length > 0){
                const pts = street.find(t => t.value == val)
                return pts ? pts.label : ''
            }
            return ''
        },
        communityName_ch: function(){
            const street = getDict('community_names')
            let val = this.propForm.building.communityName
            if(val && street.length > 0){
                const pts = street.find(t => t.value == val)
                return pts ? pts.label : ''
            }
            return ''
        },
        manageUnit_ch: function(){
            const manageUnits = getDict('administrative_office')
            let val = this.propForm.building.manageUnit
            if(val && manageUnits.length > 0){
                const pts = manageUnits.find(t => t.value == val)
                return pts ? pts.label : ''
            }
            return ''
        },*/
        ...mapGetters({
            cityAssembly: 'getCityAssembly',
            districtCollection: 'getDistrictCollection',
        })
    },
    methods: {
        closeTab() {
            //关闭当前新增页，暂时没找到好办法
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            // this.$parent.$children[2].closeTab('current')
            //跳转
            sessionStorage.setItem('current',this.current)
            this.$router.push("/property-info-sys/building-manage");
        },
        // hxf-0903-文件自动上传查看列表
        getFiles(busId) {
            if (busId) {
                getFiles(busId, this.module).then(resp => {
                    if (resp.data.code === 0) {
                        this.loadFileList = [...resp.data.data]
                    } else {
                    }
                });
            }
        },
        handleClick(tab) {
            this.busId = this.propForm.floors[tab.index].floorId
            this.getFiles(this.busId)
        },
        // hxf-0903-文件自动上传图片移除
        /*handleRemove(file) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delFiles(file.fileId, this.module).then(resp => {
                    if (resp.data.code === 0) {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        if (this.loadFileList.length > 0) {
                            let index = this.loadFileList.findIndex(e => e.fileId === file.fileId)
                            this.loadFileList.splice(index, 1)
                        }
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败!'
                        })
                    }
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },*/
        // hxf-0903-文件自动上传下载
        handleDownload(file) {
            checkFiles(file.fileId).then(resp => {
                if (resp.data.code === 0) {
                    download('/admin/sys-file/download/' + file.fileId, null).then(response => {
                        downloadBlob(response.data, file.original)
                    })
                } else {
                    this.$message({
                        message: `文件不存在！`,
                        type: 'warning'
                    })
                }
            })
        },
        // hxf-1010-根据id获取土地信息
        handleGetLandInfo(type) {
            getLandInfo(type,this.projectId).then(res => {
                if (res.data.code === 0 && res.data.data !== null) {
                    this.landInfo = res.data.data
                }
            })
        },
        // hxf-1010-回显时对应value显示label
        getCardTypeValue(num, sum){
            let arr = sum.filter(e=>e.value === num)
            if (arr.length > 0) {
                return arr[0].label
            } else {
                return num
            }
        }
    }

}
